package com.homwee.factory;
import com.homwee.factory.util.view.FactoryMacSixView;
import com.homwee.factory.util.view.FactoryMacWriteView;
import com.homwee.tvap.common.ap.ComSettingManager;
import com.homwee.tvap.common.srvfunc.FactorySetting;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.widget.RelativeLayout;


public class FactoryWriteMacActivity extends Activity  {
	private FactoryMacWriteView mtextView;
	FactoryMacSixView factoryMacSixView;
	String  ItemName ;
	String  factoryMacStr;
	public String macStr  ;
	public ComSettingManager mcomSettingManager;
	public FactorySetting mfactorySetting;
	private RelativeLayout mRootLayout,rootLayout;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mfactorySetting =FactorySetting.getInstance(this);
        LayoutInflater vi = (LayoutInflater) this
		.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mRootLayout = (RelativeLayout)vi.inflate(
				R.layout.factory_write_mac_main,null);
		setContentView(mRootLayout);
		macStr =getIntent().getStringExtra("mac");
		if(macStr ==null){
			macStr =mfactorySetting.getMAC();
		}

        initView();
		initData();
        
       
        //mtextView=(MenuChang)findViewById(R.id.menu);    
    }
    private void initView() {
    	rootLayout = (RelativeLayout)mRootLayout.findViewById(R.id.factory_write_mac_root);
    	factoryMacSixView = (FactoryMacSixView)findViewById(R.id.factory_mac_six_view);
    
    }
    private void initData() {
    	rootLayout.bringToFront();
	    factoryMacSixView.initData(macStr);
    }
    
    private String rmInvalidChar(String inMac){		
		Log.i("factory", "rmInvalidChar in mac = " + inMac);
		String invalidChar = "|";
		String outMac;
		outMac = inMac.replace(invalidChar, "");
		Log.i("factory", "rmInvalidChar out mac = " + outMac);	    	
    	return outMac;    	
    }

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		mtextView= (FactoryMacWriteView)this.getCurrentFocus();
		switch (keyCode){
		case KeyEvent.KEYCODE_3D_MODE:
//		case KeyEvent.KEYCODE_SOURCE:
//		case KeyEvent.KEYCODE_CHANGHONGIR_SETTINGS:
		case KeyEvent.KEYCODE_MENU:
			    this.finish();
			    return true;
	       case KeyEvent.KEYCODE_DPAD_CENTER:  
	        mtextView.stopDraw();
	        macStr = factoryMacSixView.getData();
			mfactorySetting.setMAC(rmInvalidChar(macStr));
			Intent intent = new Intent();  
			intent.putExtra("macInfo", macStr);  
			this.setResult(RESULT_OK, intent);  
		    this.finish();
		    return true;
		}
		if(mtextView.flag){
			switch (keyCode){
			case KeyEvent.KEYCODE_DPAD_UP:
				mtextView.onUp();
				return true;
			case KeyEvent.KEYCODE_0:
				mtextView.onkey0();
				return true;
			case KeyEvent.KEYCODE_1:
				mtextView.onkey1();
				return true;
			case KeyEvent.KEYCODE_2:
				mtextView.onkey2();
				return true;
			case KeyEvent.KEYCODE_3:
				mtextView.onkey3();
				return true;
			case KeyEvent.KEYCODE_4:
				mtextView.onkey4();
				return true;
			case KeyEvent.KEYCODE_5:
				mtextView.onkey5();
				return true;
			case KeyEvent.KEYCODE_6:
				mtextView.onkey6();
				return true;
			case KeyEvent.KEYCODE_7:
				mtextView.onkey7();
				return true;
			case KeyEvent.KEYCODE_8:
				mtextView.onkey8();
				return true;
			case KeyEvent.KEYCODE_9:
				mtextView.onkey9();
				return true;
			case KeyEvent.KEYCODE_DPAD_DOWN:
				mtextView.onDown();
				return true;
				default:
					break;
			}
		}
		return super.onKeyDown(keyCode, event);
	}
 
}